//
//  main.m
//  06集合遍历和数组排序HomeWork
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "AreaCity.h"
#import "AreaDistrict.h"
#import "AreaProvince.h"
#import "LocCity.h"
#import "LocationProvince.h"
#import "Location.h"
#import "Zone.h"
#import "City.h"
#import "Province.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
    
//    一、
//    1.创建1个包含5个字符串元素的数组；
//    2.用for循环倒序输出所有元素；
//    3.用for...in完成所有字符串元素的拼接(生成一个新的字符串)，并输出拼接好的字符串。
//    二。
//    1.创建1个包含5个person对象的数组(姓名用英文)；
//    2.用for循环找到年龄最大的Person对象；
//    3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”
//    三、
//    1.使用第二题目中生成的数组；
//    2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
//    3.使用NSSortDescriptor，按姓名进行排序。
//    四
//    处理省市区文件中的area.txt将省市区进行分割处理, 省里包含市, 市里包含区
//    1. 先用数组字典表示
//    2. 将数组字典的处理方式转换成数组模型(Class)
//    五、拓展
//    上述作业做完, 处理Location1.txt
//    要求同第四题。自己寻找规律
//    六、如果都做完额外找我要作业, 练习有的是, 就怕你不练
//
    
    //1
#if 0
    NSArray *arr1 = @[@"ab",@"cd",@"ef",@"gh",@"ij"];
    for (NSInteger i = arr1.count - 1; i >= 0; i--) {
//        NSLog(@"%@",arr1[i]);
    }
    
    NSString *str = [NSString string];
    for (NSString *str1 in arr1) {
        str = [str stringByAppendingString:str1];
    }
    NSLog(@"%@",str);
#endif
    
    //2
#if 0

    Person *per1 = [[Person alloc] initWithName:@"a" age:2 sex:@"nan"];
    Person *per2 = [[Person alloc] initWithName:@"c" age:4 sex:@"nv"];
    Person *per3 = [[Person alloc] initWithName:@"b" age:3 sex:@"nan"];
    Person *per4 = [[Person alloc] initWithName:@"e" age:1 sex:@"nv"];
    Person *per5 = [[Person alloc] initWithName:@"d" age:5 sex:@"nan"];
    NSArray *arr2 = @[per1,per2,per3,per4,per5];
    
    NSInteger max = 0;
    for (NSInteger i = 0; i < arr2.count; i++) {
        max = max > [arr2[i] age] ? max : [arr2[i] age];
    }
    for (NSInteger i = 0;i < arr2.count ; i++) {
        if ([arr2[i] age] == max) {
//            NSLog(@"%@",[arr2[i] name]);
        }
    }
    
    
    for (Person *str1 in arr2) {
        if ([str1 age] > 2) {
            NSString *newName = [[str1 name] stringByAppendingString:@"-brother"];
//            NSLog(@"%@",newName);
        }
    }
#endif
    //3.
#if 0
    NSArray *newArray1 = [arr2 sortedArrayUsingSelector:@selector(compareByName:)];
    NSArray *newArray2 = [arr2 sortedArrayUsingSelector:@selector(compareByAge:)];
    NSArray *newArray3 = [arr2 sortedArrayUsingSelector:@selector(compareBySex:)];
//    NSLog(@"%@",newArray1);
    NSLog(@"%@",newArray2);
//    NSLog(@"%@",newArray3);
    
   
    NSSortDescriptor *perSort = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
    arr2 = [arr2 sortedArrayUsingDescriptors:@[perSort]];
//    NSLog(@"%@",arr2);
#endif
    //4
#if 0
    NSString *path = @"/Users/dllo/Downloads/Homework-05/area.txt";
    NSString *areaSource = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"%@",areaSource);
    
    NSArray *lineArr = [areaSource componentsSeparatedByString:@"\n"];
    
    NSLog(@"%@",lineArr);
    NSMutableArray *provinceArr = [NSMutableArray array];
    for (NSString *line in lineArr) {
        if ([line hasPrefix:@" "] == NO) {
            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
            [provinceDic setObject:line forKey:@"ProvinceName"];
            NSMutableArray *cityArr = [NSMutableArray array];
            [provinceDic setObject:cityArr forKey:@"cityGroup"];
            [provinceArr addObject:provinceDic];
//            NSLog(@"%@",provinceDic);
//            NSLog(@"%@",provinceArr);
        //区所有的市
        }else if ( [line hasPrefix:@"  "] == YES && [line hasPrefix:@"    "] == NO){
            //创建市字典（name和区array）
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:[line substringFromIndex:2] forKey:@"cityName"];
            NSMutableArray *districtArr = [NSMutableArray array];
            [cityDic setObject:districtArr forKey:@"districtGroup"];//
            [provinceArr.lastObject[@"cityGroup"] addObject:cityDic];
//            NSLog(@"%@",cityDic);
//            NSLog(@"%@",districtArr);
//            NSLog(@"%@",provinceArr);
        }
        //取到所有的区/县
        else{
            //最后一个市
            //通过省数组获取到当前所在省的字典
            [[[provinceArr lastObject][@"cityGroup"] lastObject][@"districtGroup"]addObject:[line substringFromIndex:4]];//最终将区、县添加到数组中
        }
    }
    NSLog(@"%@",provinceArr);
    
    
    //省数组-省字典-市数组-市字典-区数组-区字典
    //省数组-Province类
    
    NSMutableArray *allArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [allArray addObject:pro];
        } else if (![area hasPrefix:@"  "] && ![area hasPrefix:@"    "]){
            Province *pro = [allArray lastObject];
            City *city = [City zoneWithName:area];
            [pro.array addObject:city];
        } else {
            City *city = [[[allArray lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }
#endif
    
            
  //5.
#if 1
    
    NSString *pathLocation = @"/Users/dllo/Downloads/Homework-05/Location1.txt";
    NSString *locationSource = [NSString stringWithContentsOfFile:pathLocation encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"%@",locationSource);
    
    NSArray *allCityArray = [locationSource componentsSeparatedByString:@","];
//    NSLog(@"%@",allCityArray);
    NSMutableArray *allCityDicArray = [NSMutableArray array];
    //根据每个城市的|处理字符串，重新生成新的字典
    for (NSString *city in allCityArray) {
        //根据|切割字符串
        NSArray *arrayCity = [city componentsSeparatedByString:@"|"];
//        NSLog(@"%@",arrayCity);
        //根据切割生成的数组，生成新的字典
        NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject],@"cityName",[arrayCity lastObject],@"cityNumber",nil];
        [allCityDicArray addObject:cityDic];
    }
//     NSLog(@"%@",allCityDicArray);
    //排序（重要）
    
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
        [allCityDicArray sortUsingDescriptors:@[sort]];
//     NSLog(@"%@",allCityDicArray);
    NSMutableArray *finalArray = [NSMutableArray array];
    for (NSMutableDictionary *cityDic in allCityDicArray) {
        NSString *cityName = cityDic[@"cityName"];
        NSString *cityNumber = cityDic[@"cityNumber"];
        if ([cityNumber hasSuffix:@"0000"]) {
            NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
            [finalArray addObject:proDic];
            
//            NSLog(@"%@",finalArray);
//            NSLog(@"省");
        } else if ([cityNumber hasSuffix:@"00"] && ![cityNumber hasSuffix:@"0000"]) {
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArrray"];
            NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [dicCity setObject:[NSMutableArray array] forKey:@"zoneArray"];
            [cityArray addObject:dicCity];
 
        } else {
//            NSMutableArray *zoneArray = [[[[finalArray lastObject] objectForKey:@"cityArray"] lastObject] objectForKey:@"zoneArray"];
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [cityArray lastObject];
            NSMutableArray *zoneArray = [dicCity objectForKey:@"zoneArray"];
            NSMutableDictionary *zoneDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [zoneArray addObject:zoneDic];
    }
#endif

    
//    NSMutableArray *allTempCityArray = [NSMutableArray array];
//    
//    for (NSString *cityString in allCityArray) {
//        NSArray *city = [cityString componentsSeparatedByString:@"|"];
////        NSLog(@"%@",city);
//        TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] cityNumber:[city lastObject]];
////        NSLog(@"%@",tempCity);
//        [allTempCityArray addObject:tempCity];
//    }
////    NSLog(@"%@",allTempCityArray);
//    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
//    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
//    
//    NSLog(@"%@",allTempCityArray);
//    for (TempCity *tempCity in allTempCityArray) {
//        if ([tempCity.number hasSuffix:@"0000"]) {
//            Province *pro = [Province zoneWithName:tempCity];
//        }
//    }
    
    
    
    
    
    
   
    
    
    
    
    
    
    
    
    
    return 0;
}
